package com.ydtg.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ydtg.bean.Customer;
import com.ydtg.dao.BaseDao;
import com.ydtg.dao.CustomerDao;

public class CustomerDaoImpl extends BaseDao implements CustomerDao{
	public List<Customer> getall(Customer news) {
		// TODO 自动生成的方法存根
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Customer> list = new ArrayList<Customer>();
		
		try {
			conn = getconnection();
			String sql = "select id,name,phone,park,burial from customer where 1=1";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Customer pet  = new Customer();
				pet.setId(rs.getInt(1));
				pet.setName(rs.getString(2));
				pet.setPhone(rs.getString(3));
				pet.setPark(rs.getInt(4));
				pet.setBurial(rs.getInt(5));
				list.add(pet);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			this.closeAll(conn, stmt, rs);
		}
		
		return list;
	}

	@Override
	public int add(Customer pet) {
		
		String psql = "insert into customer(name,phone,park,burial) "
				+ "values(?,?,?,?)";
		Object[] params = {pet.getName(),pet.getPhone(),pet.getPark(),pet.getBurial()};
		
		int count=this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public int updateByID(Customer pet) {
		
		String psql = "	UPDATE customer set name=?,phone=?,park=?,burial=? where id=?";
		Object[] params = {pet.getName(),pet.getPhone(),pet.getPark(),pet.getBurial(),pet.getId()};
		int count =  this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public int delByID(int id) {
		String psql = "	delete from customer where id=?";
		Object[] params = {id};
		int count = this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public List<Customer> getTypefaceList(Customer pet) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Customer> list = new ArrayList<Customer>();
		try {
			conn = getconnection();
			String psql = "select id,name,phone,park,burial from customer where 1=1";
			if (pet!=null&&pet.getId()!=0) {
				psql = psql+" and id="+pet.getId();
			}
			if (pet!=null&&pet.getName()!=null&&!"".equals(pet.getName())) {
				psql = psql+" and name="+pet.getName();
			}
			if (pet!=null&&pet.getPhone()!=null&&!"".equals(pet.getPhone())) {
				psql = psql+" and phone="+pet.getPhone();
			}
			if (pet!=null&&pet.getPark()!=0) {
				psql = psql+" and park="+pet.getPark();
			}
			if (pet!=null&&(pet.getBurial()==1||pet.getBurial()==0)) {
				psql = psql+" and burial="+pet.getBurial();
			}
			stmt = conn.prepareStatement(psql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Customer pet1  = new Customer();
				pet1.setId(rs.getInt(1));
				pet1.setName(rs.getString(2));
				pet1.setPhone(rs.getString(3));
				pet1.setPark(rs.getInt(4));
				pet1.setBurial(rs.getInt(5));
				list.add(pet1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			closeAll(conn, stmt, null);
		}
		return list;
	}
}
